package com.seek.educenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seek.commonutils.MD5;
import com.seek.commonutils.ResponseResult;
import com.seek.educenter.entity.UcenterMember;
import com.seek.educenter.entity.vo.UCenterQuery;
import com.seek.educenter.service.UcenterMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: wml
 * @since: 2022/3/21 0:40
 */
@RestController
@RequestMapping("/educenter/member/admin")
public class UCenterMemberAdminController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    // 条件查询带分页的方法
    @GetMapping("pageCondition/{current}/{size}")
    public ResponseResult pageCondition(@PathVariable int current, @PathVariable int size,
                                        UCenterQuery uCenterQuery) {
        Page<UcenterMember> page = new Page<>(current, size);
        // 构建条件
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        String mobile = uCenterQuery.getMobile();
        String username = uCenterQuery.getUsername();
        String nickname = uCenterQuery.getNickname();
        Integer sex = uCenterQuery.getSex();
        Integer age = uCenterQuery.getAge();
        Boolean isDisabled = uCenterQuery.getIsDisabled();
        String begin = uCenterQuery.getBegin();
        String end = uCenterQuery.getEnd();
        // 判断条件值是否为空，如果不为空拼接条件
        if (!(mobile == null || mobile.equals(""))) {
            queryWrapper.like("mobile", mobile);
        }
        if (!(username == null || username.equals(""))) {
            queryWrapper.like("username", username);
        }
        if (!(nickname == null || nickname.equals(""))) {
            queryWrapper.like("nickname", nickname);
        }
        if (sex != null) {
            queryWrapper.eq("sex", sex);
        }
        if (age != null) {
            queryWrapper.eq("age", age);
        }
        if (isDisabled != null) {
            queryWrapper.eq("is_disabled", isDisabled);
        }
        if (!(begin == null || begin.equals(""))) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!(end == null || end.equals(""))) {
            queryWrapper.le("gmt_create", end);
        }
        queryWrapper.orderByDesc("gmt_create");
        ucenterMemberService.page(page, queryWrapper);
        long total = page.getTotal();
        List<UcenterMember> records = page.getRecords();
        return ResponseResult.ok().data("total", total).data("records", records);
    }

    // 添加用户接口的方法
    @PostMapping("add")
    public ResponseResult add(@RequestBody UcenterMember ucenterMember) {
        ucenterMember.setPassword(MD5.encrypt("111111"));
        boolean result = ucenterMemberService.save(ucenterMember);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

    // 逻辑删除用户的方法
    @DeleteMapping("delete/{id}")
    public ResponseResult removeUcenterMember(@PathVariable String id) {
        boolean result = ucenterMemberService.removeById(id);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

    // 用户修改功能
    @PutMapping("update")
    public ResponseResult updateById(UcenterMember ucenterMember) {
        boolean result = ucenterMemberService.updateById(ucenterMember);
        if (result) {
            return ResponseResult.ok();
        } else {
            return ResponseResult.error();
        }
    }

    // 根据用户 id 进行查询
    @GetMapping("{id}")
    public ResponseResult getById(@PathVariable String id) {
        UcenterMember ucenterMember = ucenterMemberService.getById(id);
        return ResponseResult.ok().data("ucenterMember", ucenterMember);
    }

    // 查询所有用户
    @GetMapping
    public ResponseResult getAllUser() {
        List<UcenterMember> ucenterMemberList = ucenterMemberService.list();
        return ResponseResult.ok().data("ucenterMemberList", ucenterMemberList);
    }

    // 统计一个月以内注册的人数，按天数统计
    @GetMapping("getRegistryMonth")
    public ResponseResult getRegistryMonth() {
        Map<String, Object> map = ucenterMemberService.getRegistryMonth();
        return ResponseResult.ok().data(map);
    }

    // 统计一年内注册的人数，按月数统计
    @GetMapping("getRegistryYear")
    public ResponseResult getRegistryYear() {
        Map<String, Object> map = ucenterMemberService.getRegistryYear();
        return ResponseResult.ok().data(map);
    }
}
